/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;
//import dao.BaseDAO;
import dao.CuaHangDAO;
import java.awt.Component;
import java.util.*;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JOptionPane;
//import org.postgresql.translation.messages_bg;
import pojo.CuaHang;

/**
 *
 * @author hieupham
 */
public class DanhSachCuaHangGui extends javax.swing.JPanel {

    /**
     * Creates new form DanhSachCuaHangGui
     */
    private List<CuaHang> cuaHangs;
   // private List<CuaHang>CuaHanngList;    
    private JErrorProvider errCuaHang;
    public DanhSachCuaHangGui() throws Exception {
        initComponents();
        SetTabOrder();
        Autoload();
    }
private void SetTabOrder(){
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy=new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{txtMaCH,txtTenCH,txtDiaChi,btnTimkiem,btnThem,btnUpdate,btnDel,btnClear});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }
 private void lamsach(){        
       txtMaCH.setText("");
       txtDiaChi.setText("");
      txtTenCH.setText("");
       txtMaCH.setText("");
       txtTen_CH.setText("");
       txtDiaChi_CH.setText("");
       txtDienThoai_CH.setText("");
    } 
    private int KiemTraTruocKhiThem(){
        if(txtTen_CH.getText().trim().isEmpty()==true){
            errCuaHang.setError(txtTen_CH, "required");
            return 1;
        }
        if(txtDiaChi_CH.getText().trim().isEmpty()==true){
            errCuaHang.setError(txtDiaChi_CH, "required");      
            return 1;
        }
        return 0;
    }
    private void Autoload()throws Exception{
     cuaHangs=(List<CuaHang>) CuaHangDAO.LayDanhSach(CuaHang.class,MainGUI.nguoiDung);
     Utility.CreateColumnsFromDB(CuaHang.class, cuaHangs, tbDanhsachcuahang, new String[]{"Mã cửa hàng","Tên tên cửa hàng","Địa chỉ","Điện thoại"}, null);          
     txtMa_CH.setDragEnabled(false);
      
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbDanhsachcuahang = new org.jdesktop.swingx.JXTable();
        jPanel2 = new javax.swing.JPanel();
        btnTimkiem = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtMaCH = new javax.swing.JTextField();
        txtDiaChi = new javax.swing.JTextField();
        txtTenCH = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        btnUpdate = new javax.swing.JButton();
        btnThem = new javax.swing.JButton();
        txtMa_CH = new javax.swing.JTextField();
        txtTen_CH = new javax.swing.JTextField();
        txtDiaChi_CH = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtDienThoai_CH = new javax.swing.JTextField();
        btnDel = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("jButton1");

        setMaximumSize(new java.awt.Dimension(1024, 1024));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh Sách Cửa Hàng"));

        tbDanhsachcuahang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhsachcuahang.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        tbDanhsachcuahang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhsachcuahangMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbDanhsachcuahang);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông Tin Tìm Kiếm"));

        btnTimkiem.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnTimkiem.setText("Tìm");
        btnTimkiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimkiemActionPerformed(evt);
            }
        });

        btnClear.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel3.setText("Địa chỉ");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel2.setText("Tên cửa hàng");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel1.setText("Mã cửa hàng");

        txtMaCH.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        txtDiaChi.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        txtDiaChi.setToolTipText("dữ liệu là chuổi kí tự");

        txtTenCH.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        txtTenCH.setToolTipText("dữ liệu tìm là chuổi kí tự");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(txtMaCH, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(txtTenCH)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnTimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMaCH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTimkiem))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnClear)
                    .addComponent(txtTenCH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Cập Nhật"));

        btnUpdate.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnUpdate.setText("Sửa");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnThem.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        txtMa_CH.setEditable(false);
        txtMa_CH.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        txtTen_CH.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        txtDiaChi_CH.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel4.setText("Mã Cửa Hàng");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel5.setText("Tên Cửa Hàng");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel6.setText("Địa Chỉ ");

        jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel7.setText("Điện Thoại");

        txtDienThoai_CH.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        btnDel.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnDel.setText("Xóa");
        btnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnThem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btnDel, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(30, 30, 30)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtMa_CH)
                            .addComponent(txtTen_CH)
                            .addComponent(txtDiaChi_CH)
                            .addComponent(txtDienThoai_CH, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMa_CH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTen_CH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDiaChi_CH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(3, 3, 3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtDienThoai_CH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnThem)
                    .addComponent(btnDel))
                .addGap(6, 6, 6))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        // TODO add your handling code here:
           lamsach();
    }//GEN-LAST:event_btnClearActionPerformed

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
                    
         try{                
            if(KiemTraTruocKhiThem()>0)
                return ;        
          //  errCuaHang.clear();                      
            CuaHang cuaHang;                        
            cuaHang = new CuaHang(0,txtTen_CH.getText().trim(), txtDiaChi_CH.getText().trim(), txtDienThoai_CH.getText().trim(),false,new HashSet(),new HashSet(),new HashSet(),new HashSet(),new HashSet());           
             CuaHangDAO.ThemCuaHang(cuaHang);             
             cuaHangs.add(cuaHang);            
             Utility.CreateColumnsFromDB(CuaHang.class, cuaHangs, tbDanhsachcuahang,new String[]{"Mã cửa hàng","Tên Cửa Hàng", "Địa Chỉ","Điện Thoại"},null);                          
             tbDanhsachcuahang.changeSelection(tbDanhsachcuahang.getModel().getRowCount()-1, 0, true, false);
             JOptionPane.showMessageDialog(this, "thêm thành công !");
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this, "that bai"/*ex.getMessage()*/);
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
       Object []temp =Utility.GetSelectedRows(tbDanhsachcuahang, cuaHangs);          
       if(temp.length!=0){           
          //  if(JOptionPane.showConfirmDialog(this, "hãy chắn rằng muốn xóa cửa hàng này ", "Question", JOptionPane.YES_NO_OPTION)==0){
                try{                                                   
                    int vt =cuaHangs.indexOf(temp[0]);
                    CuaHang cuaHang =cuaHangs.get(vt);                
                    cuaHang.setTenCuaHang(txtTen_CH.getText().trim());
                    cuaHang.setDiaChi(txtDiaChi_CH.getText().trim());
                    cuaHang.setDienThoai(txtDienThoai_CH.getText().trim());
                    CuaHangDAO.SuaCuaHang(cuaHang);
                    //Utility.RemoveSelectedRows(CuaHang.class,tbDanhsachcuahang,cuaHangs,new String[]{"Mã cửa hàng","Tên Cửa Hàng", "Địa Chỉ","Điện Thoại"},null);                                                                            
                    Utility.CreateColumnsFromDB(CuaHang.class, cuaHangs, tbDanhsachcuahang,new String[]{"Mã cửa hàng","Tên Cửa Hàng", "Địa Chỉ","Điện Thoại"},null);                                                  
                //    Utility.ClearControlInPanel(null);
                    JOptionPane.showMessageDialog(this,"sửa thành công");    
                     
                } 
                catch(Exception ex){
                    JOptionPane.showMessageDialog(this, ex.getMessage());
                }                    
          //  }            
       }
       else{
           JOptionPane.showMessageDialog(this, "hãy chọn cửa hàng cần sửa bằng cách click down trên cửa hàng dưới lưới");
       }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDelActionPerformed
       Object []temp =Utility.GetSelectedRows(tbDanhsachcuahang, cuaHangs);          
       if(temp.length!=0){           
            if(JOptionPane.showConfirmDialog(this, "hãy chắn rằng muốn xóa cửa hàng này ", "Question", JOptionPane.YES_NO_OPTION)==0){
                try{                                                   
                    int vt =cuaHangs.indexOf(temp[0]);
                    CuaHang cuaHang =cuaHangs.get(vt);
                    cuaHang.setIsDelete(Boolean.TRUE);     
                    CuaHangDAO.XoaCuaHang(temp);
                    Utility.RemoveSelectedRows(CuaHang.class,tbDanhsachcuahang,cuaHangs,new String[]{"Mã cửa hàng","Tên Cửa Hàng", "Địa Chỉ","Điện Thoại"},null);                                                                                                                    
                  //  Utility.ClearControlInPanel(null);
                    JOptionPane.showMessageDialog(this,"xóa thành công");    
                    lamsach();
                } 
                catch(Exception ex){
                    JOptionPane.showMessageDialog(this, ex.getMessage());                
                }                    
            }            
       }
       else{
           JOptionPane.showMessageDialog(this, "hãy chọn cửa hàng cần xóa bằng cách click lên cửa hàng dưới lưới");
       }
    }//GEN-LAST:event_btnDelActionPerformed

    private void SetDataCuaHangToControls(CuaHang cuaHang)
    {
        String mach =String.valueOf(cuaHang.getId());
        txtMa_CH.setText(mach);
        txtTen_CH.setText(cuaHang.getTenCuaHang());
        txtDiaChi_CH.setText(cuaHang.getDiaChi());
        txtDienThoai_CH.setText(cuaHang.getDienThoai());
    }
    private void tbDanhsachcuahangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhsachcuahangMouseClicked
        Object []temp =Utility.GetSelectedRows(tbDanhsachcuahang, cuaHangs);
        if(temp.length>0){
            CuaHang cuaHang =(CuaHang)temp[0];
            SetDataCuaHangToControls(cuaHang);
          //  cuaHangs =new ArrayList<CuaHang>(String.valueOf(cuaHang.getId()));
             Utility.CreateColumnsFromDB(CuaHang.class, cuaHangs, tbDanhsachcuahang,new String[]{"Mã cửa hàng","Tên Cửa Hàng", "Địa Chỉ","Điện Thoại"},null);                          
             //Utility.ClearControlInPanel(jPanel3);
             errCuaHang.clear();
        }
        
    }//GEN-LAST:event_tbDanhsachcuahangMouseClicked

    private void btnTimkiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimkiemActionPerformed
        try {
            // TODO add your handling code here:
            
            Integer maCuaHang = txtMaCH.getText().isEmpty() ? null : Integer.parseInt(txtMaCH.getText());
            cuaHangs = CuaHangDAO.TimKiemCuaHang(maCuaHang, txtTenCH.getText().trim(),txtDiaChi.getText().trim(),false);
            Utility.CreateColumnsFromDB(CuaHang.class, cuaHangs, tbDanhsachcuahang, new String[]{"Mã cửa hàng","Tên Cửa Hàng", "Địa Chỉ","Điện Thoại"},null);
            if (cuaHangs.isEmpty()) {
                JOptionPane.showMessageDialog(this, "Không tìm thấy");
            }
        } catch (Exception ex) {
        //    logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, "dữ liệu tìm không hợp lệ");
        }
    }//GEN-LAST:event_btnTimkiemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnDel;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnTimkiem;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private org.jdesktop.swingx.JXTable tbDanhsachcuahang;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtDiaChi_CH;
    private javax.swing.JTextField txtDienThoai_CH;
    private javax.swing.JTextField txtMaCH;
    private javax.swing.JTextField txtMa_CH;
    private javax.swing.JTextField txtTenCH;
    private javax.swing.JTextField txtTen_CH;
    // End of variables declaration//GEN-END:variables
}
